package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.WorkOrderInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MaintenanceOrderInfo;
import com.ruoyi.system.service.IMaintenanceOrderInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 维修单信息Controller
 * 
 * @author LK
 * @date 2024-06-22
 */
@RestController
@RequestMapping("/system/maintenance_order_info")
public class MaintenanceOrderInfoController extends BaseController
{
    @Autowired
    private IMaintenanceOrderInfoService maintenanceOrderInfoService;

    /**
     * 查询维修单信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:maintenance_order_info:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaintenanceOrderInfo maintenanceOrderInfo)
    {
        startPage();
        List<MaintenanceOrderInfo> list = maintenanceOrderInfoService.selectMaintenanceOrderInfoList(maintenanceOrderInfo);
        return getDataTable(list);
    }

    /**
     * 导出维修单信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:maintenance_order_info:export')")
    @Log(title = "维修单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaintenanceOrderInfo maintenanceOrderInfo)
    {
        List<MaintenanceOrderInfo> list = maintenanceOrderInfoService.selectMaintenanceOrderInfoList(maintenanceOrderInfo);
        ExcelUtil<MaintenanceOrderInfo> util = new ExcelUtil<MaintenanceOrderInfo>(MaintenanceOrderInfo.class);
        util.exportExcel(response, list, "维修单信息数据");
    }

    /**
     * 获取维修单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:maintenance_order_info:query')")
    @GetMapping(value = "/{maintenanceOrderId}")
    public AjaxResult getInfo(@PathVariable("maintenanceOrderId") String maintenanceOrderId)
    {
        return success(maintenanceOrderInfoService.selectMaintenanceOrderInfoByMaintenanceOrderId(maintenanceOrderId));
    }


    /**
     * 新增维修单信息
     */
    @PreAuthorize("@ss.hasPermi('system:maintenance_order_info:add')")
    @Log(title = "维修单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaintenanceOrderInfo maintenanceOrderInfo)
    {
        MaintenanceOrderInfo maintenanceOrder = new MaintenanceOrderInfo();
        String customerId = maintenanceOrderInfo.getCustomerId();
        maintenanceOrder.setCustomerId(customerId);
        // 不允许一个用户有两个以上的工作单
        List<MaintenanceOrderInfo> maintenanceOrderLists = maintenanceOrderInfoService.selectMaintenanceOrderInfoList(maintenanceOrder);
        if(maintenanceOrderLists.size()>0){
            return AjaxResult.success();
        }else {
            return toAjax(maintenanceOrderInfoService.insertMaintenanceOrderInfo(maintenanceOrderInfo));
        }
    }


    /**
     * 批量新增维修单信息
     */
    @PreAuthorize("@ss.hasPermi('system:maintenance_order_info:add')")
    @Log(title = "维修单信息", businessType = BusinessType.INSERT)
    @PostMapping("/addMaintenance_order_info_Batch")
    public AjaxResult addMaintenance_order_info_Batch(@RequestBody List<MaintenanceOrderInfo> maintenanceOrderInfoList)
    {

        int length = maintenanceOrderInfoList.size();
        for (int i = 0; i < length; i++) {
            MaintenanceOrderInfo maintenanceOrderInfo = maintenanceOrderInfoList.get(i);
            String customerId = maintenanceOrderInfo.getCustomerId();
            String maintenanceOrderId = maintenanceOrderInfo.getMaintenanceOrderId();

            MaintenanceOrderInfo maintenanceOrder = new MaintenanceOrderInfo();
            maintenanceOrder.setCustomerId(customerId);
            maintenanceOrder.setMaintenanceOrderId(maintenanceOrderId);
            // 不允许一个用户有两个以上的工作单
            List<MaintenanceOrderInfo> maintenanceOrderLists = maintenanceOrderInfoService.selectMaintenanceOrderInfoList(maintenanceOrder);
            if(maintenanceOrderLists.size()>0){
                continue;
            }else {
               maintenanceOrderInfoService.insertMaintenanceOrderInfo(maintenanceOrderInfo);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改维修单信息
     */
    @PreAuthorize("@ss.hasPermi('system:maintenance_order_info:edit')")
    @Log(title = "维修单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaintenanceOrderInfo maintenanceOrderInfo)
    {
        return toAjax(maintenanceOrderInfoService.updateMaintenanceOrderInfo(maintenanceOrderInfo));
    }

    /**
     * 删除维修单信息
     */
    @PreAuthorize("@ss.hasPermi('system:maintenance_order_info:remove')")
    @Log(title = "维修单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{maintenanceOrderIds}")
    public AjaxResult remove(@PathVariable String[] maintenanceOrderIds)
    {
        return toAjax(maintenanceOrderInfoService.deleteMaintenanceOrderInfoByMaintenanceOrderIds(maintenanceOrderIds));
    }
}
